package com.apache.bid.common;

import java.util.HashMap;
import java.util.Map;

import com.apache.uct.common.ToolsUtil;

public class Constant {

	private BidInfoMap<String> config = new BidInfoMap<String>();//系统常量
	private Map<String, Map<String, String>> objConfig = new HashMap<String, Map<String, String>>();

	private transient static Map<String, String> jingjiaMap = new HashMap<String, String>();

	/** 金额小数点位数 */
	public static final int DECIMAL_DIGITS = 2;
	public static final String WATCH_DEFAULT = "-999999999";
	public static final String ADD = "新建";
	public static final String UPDATE = "更新";
	public static final String DELETE = "删除";
	public static final String DeayDefaultTime = "2050-12-31 23:59:59";

	/**
	 * bidMode 竞价方式
	 */
	public final static String BIDMODE_JTZX = "1";//阶梯正向 1
	public final static String BIDMODE_JTFX = "2";//阶梯反向 2

	public final static String BIDMODE_YCXASC = "3";//一次性正向 3
	public final static String BIDMODE_YCXDESC = "4";//一次性反向 4

	public final static String BIDMODE_FEJJZX = "5";//份额竞价正向 5
	public final static String BIDMODE_FEJJFX = "6";//份额竞价反向6

	public final static String BIDMODE_HELAN = "7";//荷兰式竞价 7

	/**
	 * sessionType 活动类型
	 */
	public final static String SETYPE_ZT = "1";//专厅
	public final static String SETYPE_ZC = "2";//专场
	public final static String SETYPE_HD = "3";//活动

	/**
	 * bailCtrl  是否交纳保证金
	 */
	public final static String BAILCTRL_UN = "0";//不交
	public final static String BAILCTRL_GD = "1";//固定
	public final static String BAILCTRL_NUM = "2";//数量
	public final static String BAILCTRL_BL = "3";//比例

	private static Constant instance;

	private Constant() {
	}

	public void init() {
		instance = this;
	}

	public void destroy() {
		instance = null;
	}

	public static Constant getInstance() {
		if (null == instance) {
			instance = new Constant();
		}
		return (instance);
	}

	public String getJingjiaMap(String key) {
		return jingjiaMap.get(key);
	}

	public synchronized void removeJingjiaMapByKey(String key) {
		jingjiaMap.remove(key);
	}

	public synchronized void setJingjiaMap(String key, String value) {
		if (ToolsUtil.isNull(jingjiaMap.get(key))) {
			jingjiaMap.put(key, value);
		}
	}

	public void setConfig(Map<String, String> config) {
		this.config.putAll(config);
	}

	/**
	 * description:  判断是否将数据插入数据库中
	 * @return   true插入；false不插
	 */
	public boolean ifWriteDatabase() {
		String sysType = ToolsUtil.getInstance().getValueByKey("system_type");
		if ("cacheServer".equals(sysType) || "uniteServer".equals(sysType)) {
			return true;
		}
		return false;
	}

	/**
	 * description:  获取系统公共配置
	 */
	public String getConfig(String key) {
		return config.get(key);
	}

	public void setObjConfig(Map<String, Map<String, String>> objConfig) {
		this.objConfig = objConfig;
	}

	/**
	 * description:  底层数据库的判断标识 
	 */
	public String getSysDatabase() {
		return ToolsUtil.getInstance().getValueByKey("config/jdbc.properties", "database_type");
	}

	/**
	 * description:  获取活动常量值
	 */
	public String getSessionConfig(String key) {
		BidInfoMap<String> bidMap = getObjConfigMap("sessionConfig");
		if (ToolsUtil.isEmpty(bidMap) || bidMap.isEmpty()) {
			if ("statusTemp".equals(key)) {
				return "0";
			} else if ("statusAudit".equals(key)) {
				return "1";
			} else if ("statusPubslish".equals(key)) {
				return "2";
			} else if ("statusSuspend".equals(key)) {
				return "3";
			} else if ("statusPostedFail".equals(key)) {
				return "4";
			} else if ("statusStop".equals(key)) {
				return "9";
			} else {
				return "0";
			}
		} else {
			return getObjConfigMap("sessionConfig").get(key);
		}
	}

	/**
	 * description:  获取标的常量值
	 */
	public String getItemConfig(String key) {
		BidInfoMap<String> bidMap = getObjConfigMap("itemConfig");
		if (ToolsUtil.isEmpty(bidMap) || bidMap.isEmpty()) {
			if ("statusTemp".equals(key)) {
				return "0";
			} else if ("statusAudit".equals(key)) {
				return "1";
			} else if ("statusPubslish".equals(key)) {
				return "2";
			} else if ("unStart".equals(key)) {
				return "A";
			} else if ("freeOffer".equals(key)) {
				return "B";
			} else if ("limitedOffer".equals(key)) {
				return "C";
			} else if ("increase".equals(key)) {
				return "D";
			} else if ("exercise".equals(key)) {
				return "E";
			} else if ("ending".equals(key)) {
				return "Z";
			} else if ("statusSuspend".equals(key)) {
				return "3";
			} else if ("statusPostedFail".equals(key)) {
				return "4";
			} else if ("statusSuccess".equals(key)) {
				return "5";
			} else if ("statusFailure".equals(key)) {
				return "6";
			} else if ("statusStop".equals(key)) {
				return "9";
			} else {
				return "0";
			}
		} else {
			return getObjConfigMap("itemConfig").get(key);
		}
	}

	/**
	 * description:  获取申请常量值
	 */
	public String getApplyConfig(String key) {
		BidInfoMap<String> bidMap = getObjConfigMap("applyConfig");
		if (ToolsUtil.isEmpty(bidMap) || bidMap.isEmpty()) {
			if ("statusInactivated".equals(key)) {
				return "0";
			} else if ("statusActivated".equals(key)) {
				return "1";
			} else if ("bailWaitPay".equals(key)) {
				return "0";
			} else if ("bailPartPay".equals(key)) {
				return "1";
			} else if ("bailFullPay".equals(key)) {
				return "2";
			} else if ("bailRefunded".equals(key)) {
				return "3";
			} else {
				return "0";
			}
		} else {
			return getObjConfigMap("applyConfig").get(key);
		}
	}

	private BidInfoMap<String> getObjConfigMap(String mapKey) {
		BidInfoMap<String> bidMap = new BidInfoMap<String>();
		Map<String, String> map = objConfig.get(mapKey);
		if (null != map) {
			bidMap.putAll(map);
		}
		return bidMap;
	}

}
